const { invoke } = window.__TAURI__.core;
const { listen } = window.__TAURI__.event;

window.addEventListener("DOMContentLoaded", async () => {

  const playrwright_result_ul = document.querySelector("#playwright-result > ul");
  const playrwright_result = document.querySelector("#playwright-result");
  // 获取当前时间，以及前一天此刻的时间
  listen("data-task-message", (event) => data_task_message(event, playrwright_result_ul, playrwright_result));
  listen("data-task-message-progress", (event) => data_task_message_progress(event));
});

// 添加任务
async function run_playwright() {
  // 触发自定义事件
  let baseUrl = Alpine.$data(document.querySelector('[x-data]')).selectedOption
  if (baseUrl === "") {
    Swal.fire({
      title: '提示',
      text: '请选择要测试的网站',
      icon: 'warning',
      confirmButtonText: 'OK'
    });
    return;
  }
  window.dispatchEvent(new CustomEvent('playwright-started'));
  await invoke("run_playwright", { baseUrl });
}

async function clear_playwright_result() {
  const playrwright_result_ul = document.querySelector("#playwright-result > ul");
  playrwright_result_ul.innerText = "";
}
